package formal.queueStack;

import java.util.Arrays;
import java.util.Stack;

/**
 * 下一个更大元素
 *
 * @author DengYuan2
 * @create 2021-01-29 20:54
 */
public class M_503 {
    public static void main(String[] args) {
//        int[] a = {5,4,3,2,1};
        int[] a = {1,1,1,1,1};
        int[] res = nextGreaterElements(a);
        System.out.println(Arrays.toString(res));
    }

    /**
     *做错了，按照大神的改的
     * @param nums
     * @return
     */
    public static int[] nextGreaterElements(int[] nums) {
        int length = nums.length;
        int[] res = new int[length];
        Arrays.fill(res,-1);
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 2 * length; i++) {
            int num = nums[i % length];
            while (!stack.isEmpty() && nums[stack.peek()] < num) {
                Integer cur = stack.pop();
                res[cur] = num;
            }

            if (i<length){
                stack.push(i);
            }
            //我直接这么写，也可以，只不过while里面会多判断几次吧
//            stack.push(i%length);

        }
        return res;
    }

}
